var util = require('../../../utils/util.js');  
var mathUtil = require('../../../utils/math.js');
var pageObject = {
  data: {
    disabled: false,
    plain: false,
    loading: false,
    cardId:'',
    searchLogList: [],
    showLogList: [],
    showSearchLog: false,
    
  },


  onShow: function () {
    var t = this;
    
    wx.getStorage({
      key: 'cardNum',
      success: function (res) {
        console.log("获取缓存的cardNum:" + res.data)
        if (res.data != null && res.data != '') {
          t.setData({
            cardId: res.data
          });
        }
      }
    });
    wx.getStorage({
      key: 'logCache',
      success: function (res) {
        if (res.data != null && res.data.length > 0) {
          t.setData({
            searchLogList: res.data,
            showLogList: res.data
          });
        }
      },
    });
    

  },

  cardIdInput: function (e) {
    var result = mathUtil.wordsContains(this.data.searchLogList, e.detail.value);
    console.log("触发" + result)
    this.setData({
      cardId: e.detail.value,
      showLogList: result,
    });
    if (result == null || result.length <= 0) {
      this.setData({
        showSearchLog: false,
      });
    } else {
      this.setData({
        showSearchLog: true,
      });
    }
  },

  focusOnPut: function (e) {
    var data = this.data.searchLogList;
    if (data != null && data.length > 0) {
      this.setData({
        showSearchLog: true,
        showLogList: data
      });
    }
  },

  card_function : function(e) {
    var cardId = this.data.cardId;
    console.log(this.data.cardId);
    //卡号长度判断
    if (cardId.length != 10) {
      wx.showModal({
        title: '错误',
        content: "卡号长度为10",
        success: function (res) {
          if (res.confirm) {
          }
        }
      });
    } else {
      var dateTime = util.formatTime(new Date());
      //缓存
      console.log("缓存的cardNum:" + cardId)
      wx.setStorage({
        key: "cardNum",
        data: cardId
      });
      var logList = this.data.searchLogList;
      if (cardId != null && cardId != '' && !mathUtil.isArrayContains(logList, cardId)) {
        logList.push(cardId);
        this.setData({
          searchLogList: logList
        });
        wx.setStorage({
          key: "logCache",
          data: logList
        });
      }
      console.log(dateTime);
      wx.request({
        url: 'https://wxtest.gzyct.com:443/gw/base', //仅为示例，并非真实的接口地址
        method: 'POST',
        data: {
          version: 1.0,
          service: "yct.base.card.upgrade.query",
          channel_code: "70000023",
          user_id: "18611001106",
          timestamp: dateTime,
          charset: "UTF-8",
          sign_type: "MD5",
          cardnum: "510000" + cardId,
          rt_flag: "1",
          sign: "D6B9AC55D193DC18148D0A535875DF00"
        },

        header: {
          'content-type': 'application/json' // 默认值
        },

        success: function (res) {
          var data = res.data;
          console.log(res.data)
          if (data.err_msg == '') {
            console.log(this)
            wx.navigateTo({
              url: '/pages/yct/card-result/result-display?card_num=' + cardId + '&timestamp=' + data.timestamp + '&statuscode=' + data.statuscode,
            })

          }
          else {
            wx.showModal({
              title: '卡号不正确',
              content: '请重新输入',
              success: function (res) {
                if (res.confirm) {
                }
              }
            })

          }
        }
      })
    }
    
  },
  selectItem: function (e) {
    this.setData({
      cardId: e.currentTarget.dataset.log,
      showSearchLog: false
    })
  },
  blurOut: function (e) {
    this.setData({
      showSearchLog: false
    })
  },
  removeData: function(e){
    wx.removeStorage({
      key: 'logCache',
      success: function (res) {
        console.log('清除历史记录' + { searchLogList: res.data } + {showLogList: res.data})
      }
    })
    this.setData({
      searchLogList:[],
      showLogList:[],
      showSearchLog: false,
      cardId: '',
    })
    
    
  }
    
}

Page(pageObject);